﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Data;
using DMS.Base;
using DMS.Business.Utility;
using System.Data.Common;

namespace DMS.Web.Common.Controls
{
    public partial class ErrorLogList : System.Web.UI.UserControl
    {
        #region 成员属性
        /// <summary>
        /// 页大小限制
        /// </summary>
        public int PageLimte
        {
            get
            {
                return this.pager.PageSize;
            }
            set
            {
                this.pager.PageSize = value;
            }
        }
        #endregion

        #region 界面事件
        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.InitDataList();
            }
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageChanged(object sender, EventArgs e)
        {
            this.InitDataList();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.pager.GoToPage(1);
            this.InitDataList();
        }
        #endregion

        #region 内部方法
        public void InitDataList()
        {
            int total = 0;
            string condition = string.Empty;
            List<DbParameter> dbParams = new List<DbParameter>();

            //if (!string.IsNullOrEmpty(this.txtCode.Text))
            //{
            //    condition += " AND [ErrorID] LIKE @ErrorID";
            //    DbParameter p_ErrorCode = DBHelper.CreateParamer();
            //    p_ErrorCode.ParameterName = "ErrorID";
            //    p_ErrorCode.Value = string.Format("%{0}%", this.txtCode.Text.Trim());
            //    dbParams.Add(p_ErrorCode);
            //}

            if (!string.IsNullOrEmpty(this.txtMessage.Text))
            {
                condition += " AND [Message] LIKE @Message";
                DbParameter p_Message = DBHelper.CreateParamer();
                p_Message.ParameterName = "Message";
                p_Message.Value = string.Format("%{0}%", (object) this.txtMessage.Text.Trim());
                dbParams.Add(p_Message);
            }

            if (!string.IsNullOrEmpty(this.txtOperateUser.Text))
            {
                condition += " AND [CurrentUser] LIKE @Operater";
                DbParameter p_Operater = DBHelper.CreateParamer();
                p_Operater.ParameterName = "Operater";
                p_Operater.Value = string.Format("%{0}%", (object) this.txtOperateUser.Text.Trim());
                dbParams.Add(p_Operater);
            }

            if (!string.IsNullOrEmpty(this.txtClientIP.Text))
            {
                condition += " AND [ClinetIP] LIKE @ClinetIP";
                DbParameter p_ClinetIP = DBHelper.CreateParamer();
                p_ClinetIP.ParameterName = "ClinetIP";
                p_ClinetIP.Value = string.Format("%{0}%", (object) this.txtClientIP.Text.Trim());
                dbParams.Add(p_ClinetIP);
            }

            if (!string.IsNullOrEmpty(this.txtRequestUrl.Text))
            {
                condition += " AND [RequestURL] LIKE @RequestURL";
                DbParameter p_RequestURL = DBHelper.CreateParamer();
                p_RequestURL.ParameterName = "RequestURL";
                p_RequestURL.Value = string.Format("%{0}%", (object) this.txtRequestUrl.Text.Trim());
                dbParams.Add(p_RequestURL);
            }

            if (!string.IsNullOrEmpty(this.txtBeginTime.Text))
            {
                condition += " AND [CreateTime] >= @BeginTime";
                DbParameter p_BeginTime = DBHelper.CreateParamer();
                p_BeginTime.DbType = DbType.Date;
                p_BeginTime.ParameterName = "BeginTime";
                p_BeginTime.Value = Convert.ToDateTime((string) this.txtBeginTime.Text.Trim());
                dbParams.Add(p_BeginTime);
            }
            if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                condition += " AND [CreateTime] < @EndTime";
                DbParameter p_EndTime = DBHelper.CreateParamer();
                p_EndTime.DbType = DbType.Date;
                p_EndTime.ParameterName = "EndTime";
                p_EndTime.Value = Convert.ToDateTime((string) this.txtEndTime.Text.Trim()).AddDays(1).ToString();
                dbParams.Add(p_EndTime);
            }

            if (!string.IsNullOrEmpty(this.txtOther.Text))
            {
                condition += " AND ([HResult] LIKE @Other OR [Source] LIKE @Other OR [StackTrace] LIKE @Other OR [TargetSite] LIKE @Other OR [ClientBroswer] LIKE @Other )";
                DbParameter p_txtOther = DBHelper.CreateParamer();
                p_txtOther.ParameterName = "Other";
                p_txtOther.Value = string.Format("%{0}%", (object) this.txtOther.Text.Trim());
                dbParams.Add(p_txtOther);
            }

            DataSet ds = LogHelper.GetLogs(pager.PageSize, pager.CurrentPageIndex, out total, condition, dbParams);
            this.dgLogList.DataSource = ds;
            this.dgLogList.DataBind();

            this.pager.RecordCount = total;


            // 处理空数据
            if (total == 0)
            {
                this.dgLogList.Visible = false;
                this.pager.Visible = false;
                this.dvEmpty.Visible = true;
            }
            else
            {
                this.dgLogList.Visible = true;
                this.pager.Visible = true;
                this.dvEmpty.Visible = false;
            }
        }
        #endregion

        public string BreakLine(String srcString)
        {
            return Regex.Replace(srcString, "在 ", "<br/>在 "); ;
        }
    }
}